function ajax(url,data,s,f){
  wx.request({
    url: url,
    data:data,
    method: "post",
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
  
      s&&s(res);
    },
    fail:function(res){
      f&&f(res)
    }
  })
}



//输入e和要获取的键返回键对应的值
function getdata(e){
 return e.currentTarget.dataset
}
//星座检测输入年月日格式为2011-11-12
function checkxingzuo(a) {
  var arr = a.split("-")
  switch (arr[1]*1) {
    case 1:
      return arr[2] <= 19 ? "摩羯座" : "水瓶座";
      break;
    case 2:
      return arr[2] <= 18 ? "水瓶座" : "双鱼座";
      break;
    case 3:
      return arr[2] <= 20 ? "双鱼座" : "白羊座";
      break;
    case 4:
      return arr[2] <= 19 ? "白羊座" : "金牛座";
      break;
    case 5:
      return arr[2] <= 20 ? "金牛座" : "双子座";
      break;
    case 6:
      return arr[2] <= 21 ? "双子座" : "巨蟹座";
      break;
    case 7:
      return arr[2] <= 22 ? "巨蟹座" : "狮子座";
      break;
    case 8:
      return arr[2] <= 22 ? "狮子座" : "处女座";
      break;
    case 9:
      return arr[2] <= 23 ? "处女座" : "天秤座";
      break;
    case 10:
      return arr[2] <= 23 ? "天秤座" : "天蝎座";
      break;
    case 11:
      return arr[2] <= 22 ? "天蝎座" : "射手座";
      break;
    case 12:
      return arr[2] <= 21 ? "射手座" : "摩羯座";
      break;
  }
}

function pan(warn, status = 1) {
  if (status == 1) {
    wx.showToast({
      title: warn,
      image: '../../images/dui.png'
    })
  } else if (status == 0) {
    wx.showToast({
      title: warn,
      image: '../../images/closered.png'
    })
  }

}


function phoneVer(phone) {
  var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
  if (!phoneReg.test(phone)) {
    wx.showToast({
      title: '手机号码有误',
      image: '/images/closered.png',
    })
    wx.hideLoading()
  }
  return;
}

function loginVer(that,fun) {
  var userinfo = wx.getStorageSync('userinfo');
  if (!userinfo) {
    that.setData({hidden:false})
  }else{
    that.setData({hidden:true});
    typeof fun == "function" && fun(that)
  }
  return;
  
}

function getDateDiff(dateTimeStamp) {
  var minute = 1000 * 60;
  var hour = minute * 60;
  var day = hour * 24;
  var halfamonth = day * 15;
  var month = day * 30;
  var now = new Date().getTime();
  var diffValue = now - dateTimeStamp;
  if (diffValue < 0) { return; }
  var monthC = diffValue / month;
  var weekC = diffValue / (7 * day);
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;
  var result;
  if (monthC >= 1) {
    result = "" + parseInt(monthC) + "月前";
  }
  else if (weekC >= 1) {
    result = "" + parseInt(weekC) + "周前";
  }
  else if (dayC >= 1) {
    result = "" + parseInt(dayC) + "天前";
  }
  else if (hourC >= 1) {
    result = "" + parseInt(hourC) + "小时前";
  }
  else if (minC >= 1) {
    result = "" + parseInt(minC) + "分钟前";
  } else
    result = "刚刚";
  return result;
}

function back(backnum,status){
  if (status==1){
    wx.navigateBack({
      delta: backnum
    })
  }else{
    setTimeout(function () {
      wx.navigateBack({
        delta: backnum
      })
    }, 1000)
  }
}

function fllow(that){
  var userinfo = wx.getStorageSync('userinfo');
  if (userinfo){
    that.setData({ temhidden:true})
  }else{
    that.setData({ temhidden: false })
  }
  return;
}
//通过后台时间错 来 获取年月日格式
function format(dates) {
  console.log(dates);
  var timestamp4 = new Date(dates * 1000);
  var y = timestamp4.getFullYear();
  var m = timestamp4.getMonth() + 1;
  var d = timestamp4.getDate();
  var h = timestamp4.getHours();
  var i = timestamp4.getMinutes();
  var s = timestamp4.getSeconds();

  return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
}
module.exports={
  ajax:ajax,
  checkxingzuo:checkxingzuo,
  getdata:getdata,
  pan: pan,
  phoneVer: phoneVer,
  loginVer: loginVer,
  back: back,
  fllow: fllow,
  getDateDiff: getDateDiff,
  format: format
}